﻿using BookmarkingSite.Model;
using System;
using System.Linq;

namespace BookmarkingSite.Client
{
    public static class DatabasePersister
    {
        public static void ImportBookmarks(string username, string title, string url, string notes, string[] tags)
        {
            BookmarkingSiteEntities db = new BookmarkingSiteEntities();
            using (db)
            {
                User userEntitie = new User()
                {
                    Username = username
                };

                if (!db.Users.Any(u => u.Username == username))
                {
                    db.Users.Add(userEntitie);
                }
                else
                {
                    userEntitie = db.Users.Where(u => u.Username == username).First();
                }

                Bookmark bookmarkEntitie = new Bookmark()
                {
                    Title = title,
                    Url = url,
                    Notes = notes,
                };

                userEntitie.Bookmarks.Add(bookmarkEntitie);
                db.SaveChanges();

                foreach (var tag in tags)
                {
                    Tag tagEntitie = new Tag()
                    {
                        TagName = tag.ToLower()
                    };

                    if (!db.Tags.Any(t => t.TagName == tag.ToLower()))
                    {
                        db.Tags.Add(tagEntitie);
                    }
                    else
                    {
                        tagEntitie = db.Tags.Where(t => t.TagName == tag.ToLower()).First();
                    }

                    bookmarkEntitie.Tags.Add(tagEntitie);
                }
                db.SaveChanges();

                string[] titleTags = title.Split(' ', ',', '.', '!', '?');
                foreach (var titleTag in titleTags)
                {
                    Tag tagEntitie = new Tag()
                    {
                        TagName = titleTag.ToLower()
                    };

                    if (!db.Tags.Any(t => t.TagName == titleTag.ToLower()))
                    {
                        db.Tags.Add(tagEntitie);
                    }
                    else
                    {
                        tagEntitie = db.Tags.Where(t => t.TagName == titleTag.ToLower()).First();
                    }

                    bookmarkEntitie.Tags.Add(tagEntitie);                    
                }

                db.SaveChanges();
            }
        }

        public static void SearchUsers(string username, string tagName)
        {
            BookmarkingSiteEntities db = new BookmarkingSiteEntities();
            using (db)
            {
                if (username == null)
                {
                    if (db.Tags.Any(t => t.TagName == tagName))
                    {
                        var bookmarks = db.Tags.Where(t => t.TagName == tagName).First().Bookmarks.OrderBy(b => b.Url);
                        foreach (var bookmark in bookmarks)
                        {
                            Console.WriteLine(bookmark.Url);
                        }
                    }
                    else
                    {
                        Console.WriteLine("Nothing found");
                    }
                }
                else
                {
                    if (db.Tags.Any(t => t.TagName == tagName))
                    {
                        bool isFound = false;
                        var bookmarks = db.Tags.Where(t => t.TagName == tagName).First().Bookmarks.OrderBy(b => b.Url);
                        foreach (var bookmark in bookmarks)
                        {
                            if (bookmark.User.Username == username)
                            {
                                isFound = true;
                                Console.WriteLine(bookmark.Url);
                            }
                        }

                        if (isFound == false)
                        {
                            Console.WriteLine("Nothing found");
                        }
                    }
                    else
                    {
                        Console.WriteLine("Nothing found");
                    }
                }
            }            
        }
    }
}
